import java.awt.*;
import java.util.ArrayList;


public class Snake {
	
	ArrayList<Segment> snake;
	private int waitingSegments=0;
	private int playerNumber;
	static final int PLAYER_ONE=0, PLAYER_TWO=1; 
	static final Color[] PLAYER_ONE_COLORS={Color.RED,Color.GREEN};
	static final Color[] PLAYER_TWO_COLORS={Color.ORANGE,Color.BLUE};
	
	public Snake(int playerNumber){
		snake = new ArrayList<Segment>(4);
		this.playerNumber=playerNumber;
		/*
		for (int i=0; i<4;i++){
			snake.add(i, null);
		}
		
		for (int i=3; i>=0;i--){
			snake.add(i, new Segment(Segment.RIGHT, i+20, 10));
		}
		*/
		
		if (playerNumber==Snake.PLAYER_ONE){
			for (int i=4; i>0; i--) 
				snake.add(new Segment(Segment.RIGHT,i+7,2));
		}
		else if (playerNumber==Snake.PLAYER_TWO){
			for (int i = 0;i<4;i++)
				snake.add(new Segment(Segment.LEFT,i+24,2));
		}
		
		
	}
	
	public boolean move(){
	
		
		/* This was fun, wasn't it?
		int headDirection = snake.get(0).getDirection();
		for (int i=0; i<snake.size();i++){
			
			
			
			snake.get(i).move();
			if (snake.get(i).getDirection()!=headDirection){
				headDirection=snake.get(i).getDirection();
				snake.get(i).changeDirection(snake.get(i-1));
			}
			// snake.get(i).move();
			//No, never!
		}
		*/
		Segment cloneMe = snake.get(snake.size()-1);
		Segment clone = new Segment(cloneMe.getDirection(),cloneMe.getXPos(),cloneMe.getYPos());
		
		for (int i = 0; i<snake.size(); i++){
			snake.get(i).move();
		}
		
		for (int i = snake.size()-1; i>0; i--){
			snake.get(i).changeDirection(snake.get(i-1));
		}
		
		if (waitingSegments > 0){
			
			
			
			
			snake.add(clone);
			waitingSegments--;
		}
		
		if (isColliding()){
			return false;
		}
		return true;
	}
	
	public void changeDirection(int direction){
		snake.get(0).changeDirection(direction);
	}
	
	public boolean isColliding(){
		for (int i=0;i<snake.size();i++){
			for (int j=0;j<snake.size();j++){
				if (snake.get(i).getPoint().equals(snake.get(j).getPoint()) && i!=j)
					return true;
			}
		}
		
		for (Segment s:snake){
			if (s.getXPos()>=30 || s.getXPos()<0 || s.getYPos()>=30 || s.getYPos()<0)
				return true;
		}
		return false;
	}
	
	public boolean collidesWithSnake(Snake s){
		
		
		if (s.hasPoint(snake.get(0).getPoint()))
			return true;

		return false;
	}
	
	
	
	public ArrayList<Segment> getList(){
		return this.snake;
	}
	
	public boolean hasPoint(Point p){
		for (Segment s: snake){
			if (s.getPoint().equals(p))
				return true;
		}
		return false;
	}
	
	public void draw(Graphics g, Dimension d, Color[] snakeColors){
		g.setColor(snakeColors[0]);
		for (int i=0; i<snake.size();i++){
			
			if (snake.get(i)!=null)
				snake.get(i).draw(g,d);
			g.setColor(snakeColors[1]);
		}
	}
	
	public int getDirection(){
		return snake.get(0).getDirection();
	}
	
	public boolean eat(Food f){
		for (Segment s: snake){
			if (s.getPoint().equals(f.getPoint()))
				return true;
		}
		return false;
	}
	public void grow(){
		waitingSegments=2;
	}
	
	public String toString(){
		return "Player "+this.playerNumber+1;
	}
}
